package qst.com.bacK.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qst.com.bacK.exception.ExamException;
import qst.com.bacK.mapper.ClassTeamMapper;
import qst.com.bacK.pojo.entity.Team;
import qst.com.bacK.pojo.entity.TestPersonnel;
import qst.com.bacK.service.ClassTeamService;

import java.sql.SQLException;
import java.util.List;

/**
 * Author:   msh
 * Date:     2024/10/12 上午9:59
 * Description:
 */

@Service
public class ClassTeamServiceImpl implements ClassTeamService {
    @Autowired
    private ClassTeamMapper classTeamMapper;

    @Override
    public void deleteTeam(int id) {
        try {
            List<TestPersonnel> personnels = classTeamMapper.findByTeam(id);
            if (personnels.size() > 0) {
                throw new ExamException("有参测人员的批次不能删除");
            }
            classTeamMapper.delete(id);
        } catch (SQLException ex) {
            throw new ExamException("数据库查询出错");
        }
    }

    @Override
    public Team findById(Integer id) {
        try {
            return classTeamMapper.findById(id);
        } catch (SQLException ex) {
            throw new ExamException("数据库查询出错");
        }
    }

    @Override
    public List<Team> findAll() {
        try {
            return classTeamMapper.findAll();
        } catch (SQLException ex) {
            throw new ExamException("数据库查询出错");
        }
    }

    @Override
    public List<Team> findByCreator(Integer id) {
        try {
            return classTeamMapper.findByCreator(id);
        } catch (SQLException ex) {
            throw new ExamException("数据库查询出错");
        }
    }

    @Override
    public void saveTeam(Team t) {
        try {
            classTeamMapper.save(t);
        } catch (SQLException ex) {
            throw new ExamException("数据库查询出错");
        }
    }

    @Override
    public void updateTeam(Team t) {
        try {
            classTeamMapper.update(t);
        } catch (SQLException ex) {
            throw new ExamException("数据库查询出错");
        }
    }

}
